/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.model;

import com.aroundj2me.utility.StringUtils;
import com.aroundj2me.view.IProgressWindow;


/**
 * Class to abstract the common behavior of an executable task, i.e. a task that
 * is executed in a thread. This class facilitates the listening for thread events, as well
 * as the retrieving of the processing result/error.
 */
public abstract class ExecutableTask implements Runnable {
	protected TaskProcessListener listener;
	protected String statusMessage;
	protected IProgressWindow progressWindow;
	private String progressMessage;
	
	public ExecutableTask(){	
	}
	
	public void run(){
		if(progressWindow != null){		
			if(!StringUtils.isNullOrEmpty(progressMessage))
				progressWindow.startProgress(progressMessage);
			else
				progressWindow.startProgress();
		}
	}
	
	public ExecutableTask(TaskProcessListener listener){	
		this.listener = listener;
	}

	protected void notifyTaskFinished(Object result){
		if(progressWindow != null){
			progressWindow.stopProgress();
		}
		this.listener.taskFinished(result);
	}
	
	protected void notifyError(String statusMessage){
		if(progressWindow != null)
			progressWindow.stopProgress();
		this.listener.onError(statusMessage);
	}

	public TaskProcessListener getListener() {
		return listener;
	}

	public void setListener(TaskProcessListener listener) {
		this.listener = listener;
	}
	
	public void setProgressWindow(IProgressWindow progressWindow){
		this.progressWindow = progressWindow;
	}
	
	public void setProgressMessage(String message){
		this.progressMessage = message;
	}
	
}
